package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ActivitySummary;
import com.ruoyi.system.domain.ActivityManagement;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.domain.vo.UserUnDoVo;
import com.ruoyi.system.mapper.ActivitySummaryMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IActivityManagementService;
import com.ruoyi.system.service.IActivitySummaryService;
import com.ruoyi.system.service.IDeptFileArchivedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动纪要Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-03
 */
@Service
public class ActivitySummaryServiceImpl implements IActivitySummaryService
{
    @Autowired
    private ActivitySummaryMapper activitySummaryMapper;
    @Autowired
    private IActivityManagementService activityManagementService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;

    /**
     * 查询活动纪要
     *
     * @param id 活动纪要主键
     * @return 活动纪要
     */
    @Override
    public ActivitySummary selectActivitySummaryById(Long id)
    {
        ActivitySummary summary = activitySummaryMapper.selectActivitySummaryById(id);
        //纪要文件
        String summaryFile = summary.getSummaryFile();
        if(StringUtils.isNotBlank(summaryFile)){
            List<String> summaryFileList = Arrays.asList(summaryFile.split(","));
            summary.setSummaryFileList(summaryFileList);
        }
        //记录文件
        String recordFile = summary.getRecordFile();
        if(StringUtils.isNotBlank(recordFile)){
            List<String> recordFileList = Arrays.asList(recordFile.split(","));
            summary.setRecordFileList(recordFileList);
        }
        //纪要图片
        String summaryImage = summary.getSummaryImage();
        if(StringUtils.isNotBlank(summaryImage)){
            List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
            summary.setSummaryImageList(summaryImageList);
        }
        //活动通知
        String activityNotify = summary.getActivityNotify();
        if(StringUtils.isNotBlank(activityNotify)){
            List<String> activityNotifyList = Arrays.asList(activityNotify.split(","));
            summary.setActivityNotifyList(activityNotifyList);
        }
        //活动签到
        String activitySignature = summary.getActivitySignature();
        if(StringUtils.isNotBlank(activitySignature)){
            List<String> activitySignatureList = Arrays.asList(activitySignature.split(","));
            summary.setActivitySignatureList(activitySignatureList);
        }
        return summary;
    }

    /**
     * 根据条件查询活动纪要列表
     *
     * @param activityManagement 活动纪要
     * @return 活动纪要集合
     */
    @Override
    public List<ActivityManagement> getActivitySummaryList(ActivityManagement activityManagement){
        List<ActivityManagement> activityManagementList = activityManagementService.selectActivitySignaturelist(activityManagement);
        //已召开的活动
        List<ActivityManagement> collect = activityManagementList.stream().filter(o -> o.getStatus().equals("2") || o.getStatus().equals("3")).collect(Collectors.toList());
        for(ActivityManagement activity : collect){
            Long id = activity.getId();
            ActivitySummary summary = activitySummaryMapper.selectActivitySummaryByActivityId(id);
            activity.setActivitySummary(summary);
        }
        return collect;
    }

    /**
     * 查询活动纪要列表
     *
     * @param activitySummary 活动纪要
     * @return 活动纪要
     */
    @Override
    public List<ActivitySummary> selectActivitySummaryList(ActivitySummary activitySummary)
    {
        List<ActivitySummary> activitySummaryList = activitySummaryMapper.selectActivitySummaryList(activitySummary);
        return activitySummaryList;
    }

    /**
     * 新增活动纪要
     *
     * @param activitySummary 活动纪要
     * @return 结果
     */
    @Override
    public int insertActivitySummary(ActivitySummary activitySummary)
    {
        activitySummary.setCreateTime(DateUtils.getNowDate());
        return activitySummaryMapper.insertActivitySummary(activitySummary);
    }

    /**
     * 修改活动纪要
     *
     * @param activitySummary 活动纪要
     * @return 结果
     */
    @Override
    public int updateActivitySummary(ActivitySummary activitySummary)
    {
        ActivitySummary summary = activitySummaryMapper.selectActivitySummaryById(activitySummary.getId());
        String createBy = activitySummary.getCreateBy();
        SysUser user = sysUserMapper.selectUserById(Long.valueOf(createBy));
        String userName = user.getNickName();
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(summary.getActivityId()));
        //如果是上传之后重新编辑资料，删除归纳文件重新上传
        DeptFileArchived deptFileArchivedSummary = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(activitySummary.getId()));
        if(!ObjectUtils.isEmpty(deptFileArchivedSummary)){
            deptFileArchivedService.deleteDeptFileArchivedByAssociationId(deptFileArchivedSummary.getAssociationId());
        }
        //纪要文件
        List<String> summaryFileList = activitySummary.getSummaryFileList();
        if(!CollectionUtils.isEmpty(summaryFileList)){
            String summaryFile = StringUtils.join(summaryFileList, ",");
            activitySummary.setSummaryFile(summaryFile);
            //创建纪要文件文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("活动纪要文件");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(activitySummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建纪要文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("活动纪要文件附件");
            deptFileArchivedVo2.setFileUrl(summaryFile);
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(activitySummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //记录文件
        List<String> recordFileList = activitySummary.getRecordFileList();
        if(!CollectionUtils.isEmpty(recordFileList)){
            String recordFile = StringUtils.join(recordFileList, ",");
            activitySummary.setRecordFile(recordFile);
            //创建记录文件文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("活动记录文件");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(activitySummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建记录文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("活动记录文件附件");
            deptFileArchivedVo2.setFileUrl(recordFile);
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(activitySummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //纪要图片
        List<String> summaryImageList = activitySummary.getSummaryImageList();
        if(!CollectionUtils.isEmpty(summaryImageList)){
            String summaryImage = StringUtils.join(summaryImageList, ",");
            activitySummary.setSummaryImage(summaryImage);
            //创建纪要图片文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("活动图片");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(activitySummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建纪要图片文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("活动图片附件");
            deptFileArchivedVo2.setFileUrl(summaryImage);
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(activitySummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //活动通知
        List<String> activityNotifyList = activitySummary.getActivityNotifyList();
        if(!CollectionUtils.isEmpty(activityNotifyList)){
            String activityNotify = StringUtils.join(activityNotifyList, ",");
            activitySummary.setActivityNotify(activityNotify);
            //创建活动通知文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("活动通知表文件");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(activitySummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建活动通知文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("活动通知表附件");
            deptFileArchivedVo2.setFileUrl(activityNotify);
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(activitySummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //活动签到
        List<String> activitySignatureList = activitySummary.getActivitySignatureList();
        if(!CollectionUtils.isEmpty(activitySignatureList)){
            String activitySignature = StringUtils.join(activitySignatureList, ",");
            activitySummary.setActivitySignature(activitySignature);
            //创建活动签到文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("活动签到表文件");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(activitySummary.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建活动签到文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("活动签到表附件");
            deptFileArchivedVo2.setFileUrl(activitySignature);
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(activitySummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        //上传时间
        String summaryFile1 = summary.getSummaryFile();
        String summaryImage1 = summary.getSummaryImage();
        if(StringUtils.isBlank(summaryFile1)&&StringUtils.isBlank(summaryImage1)){
            Date nowDate = DateUtils.getNowDate();
            activitySummary.setUploadTime(nowDate);
        }
        activitySummary.setUpdateTime(DateUtils.getNowDate());
        return activitySummaryMapper.updateActivitySummary(activitySummary);
    }

    /**
     * 删除活动纪要信息
     *
     * @param id 活动纪要主键
     * @return 结果
     */
    @Override
    public int deleteActivitySummaryById(Long id)
    {
        return activitySummaryMapper.deleteActivitySummaryById(id);
    }

    /**
     * 修改活动纪要状态
     *
     * @param activitySummary 活动纪要
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateActivitySummaryStatus(ActivitySummary activitySummary){
        //修改活动纪要状态
        ActivitySummary summary = new ActivitySummary();
        summary.setId(activitySummary.getId());
        summary.setStatus(activitySummary.getStatus());
        int i = activitySummaryMapper.updateActivitySummary(summary);
        //完成和延期完成的修改活动状态为 已召开已归档
        String status = activitySummary.getStatus();
        if("1".equals(status)||"3".equals(status)){
            Long activityId = activitySummary.getActivityId();
            ActivityManagement activityManagement = new ActivityManagement();
            activityManagement.setId(activityId);
            activityManagement.setStatus("3");
            activityManagementService.updateActivityManagement(activityManagement);
        }
        return i;
    }

    /**
     * 查询活动上传待办任务
     *
     * @return 结果
     */
    @Override
    public List<UserUnDoVo> selectActivityUploadTask(UserUnDoVo userUnDoVo){
        List<UserUnDoVo> userUnDoVos = activitySummaryMapper.selectActivityUploadTask(userUnDoVo);
        return userUnDoVos;
    }
}
